package com.example.janlay.test;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Parcelable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.janlay.test.tools.ActivityManager;

public class activity_install_info extends AppCompatActivity {

    //那个权限按钮
    private Button jurisdictionBtn;

    //那个是否删除安装包开关
    private Switch isDeleteApkPackage;

    private static final String TAG = "MainActivity";

    private ImageView appIconView;

    private TextView appNameView;

    private TextView appVersionView;

    private String appName;

    private String version;

    private Drawable icon1;

    private String appUrl;

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_install_info);
        //为了防止找不到资源......写try catch
        try {
            jurisdictionBtn = findViewById(R.id.jurisdictionBtn);
            isDeleteApkPackage = findViewById(R.id.isDeleteApkPackage);
            appIconView = findViewById(R.id.appIcon);
            appNameView = findViewById(R.id.appName);
            appVersionView = findViewById(R.id.appVersion);
        } catch (Exception e) {
            Log.e(TAG, "onCreate: ", e);
        }
        appUrl = getApkPath();
        isDeleteApkPackageListener();
        verifyStoragePermissions(this);
        setApkInfo(getApkPath(), this);
    }

    /*
     *
     * App开始允许的生命周期
     * */
    @Override
    protected void onStart() {
        super.onStart();

        //修复切换前台按钮过大bug
        isDeleteApkPackage.setThumbResource(R.drawable.switch_thumb);
        isDeleteApkPackage.setTrackResource(R.drawable.switch_track);

        //第二行的箭头添加方法
        setDrawRight(jurisdictionBtn, R.mipmap.arrowright);
    }

    /*
     *
     * 图标添加事件
     *
     * */

    private void setDrawRight(Button btn, int res) {
        //先获取到drawable内的图片 入参为R文件里的文件地址
        Drawable drawable = getResources().getDrawable(res);

        // 给图片赋值大小
        drawable.setBounds(0, 0, 70, 70);

        // 给图片制定所在按钮位置 指定为Right
        btn.setCompoundDrawables(null, null, drawable, null);
    }

    /*
     *
     * Author:janlay
     * 开关监听事件
     *
     * */
    private void isDeleteApkPackageListener() {
        isDeleteApkPackage.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) { //如果选中
                    Toast.makeText(activity_install_info.this, "开关已经选中！", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(activity_install_info.this, "开关已经取消选中！", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    /*
     *
     * Author:janlay
     * 退出按钮监听事件
     *
     * */
    public void closeApp(View view) {
        // 下面这两段代码是kill app main activity ~~~~~
        ActivityManager.getInstance().addActivity(activity_install_info.this);
        ActivityManager.getInstance().exit();
    }

    /*
     *
     * Author:janlay
     * 安装按钮监听事件
     *
     * */
    public void installApp(View view) {
        Intent intent = new Intent();
        //setClass函数的第一个参数是一个Context对象
        //Context是一个类，Activity是Context类的子类，也就是说，所有的Activity对象，都可以向上转型为Context对象
        //setClass函数的第二个参数是一个Class对象，在当前场景下，应该传入需要被启动的Activity类的class对象
        intent.setClass(this, activity_install_loading.class);
        System.out.println(appUrl);
        intent.putExtra("appUrl", appUrl);
        startActivity(intent);
    }

    /**
     * 获取apk包的信息：版本号，名称，图标等
     *
     * @param absPath apk包的绝对路径
     * @param context
     */
    public void setApkInfo(String absPath, Context context) {
        PackageManager pm = context.getPackageManager();
        PackageInfo pkgInfo = pm.getPackageArchiveInfo(absPath, PackageManager.GET_ACTIVITIES);
        if (pkgInfo != null) {
            ApplicationInfo appInfo = pkgInfo.applicationInfo;
            /* 必须加这两句，不然下面icon获取是default icon而不是应用包的icon */
            appInfo.sourceDir = absPath;
            appInfo.publicSourceDir = absPath;
            appName = pm.getApplicationLabel(appInfo).toString();// 得到应用名
            String packageName = appInfo.packageName; // 得到包名
            version = pkgInfo.versionName; // 得到版本信息
            /* icon1和icon2其实是一样的 */
            icon1 = pm.getApplicationIcon(appInfo);// 得到图标信息
            appNameView.setText(appName);
            appVersionView.setText(version);
            appIconView.setImageDrawable(icon1);
        }
    }

    private String getApkPath() {
        Intent intent = getIntent();
        Uri uri = (Uri) intent.getData();
        String path = "1";
        if (uri != null) {
            path = uri.getPath();
        }
        return path;
    }

    public static void verifyStoragePermissions(Activity activity) {
        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(activity,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
